//package com.software.levelupandroid.home.ui.taskByPriority;
//
//import android.os.Bundle;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//
//import androidx.annotation.NonNull;
//import androidx.annotation.Nullable;
//import androidx.fragment.app.Fragment;
//import androidx.lifecycle.Observer;
//import androidx.lifecycle.ViewModelProvider;
//
//import com.software.levelupandroid.R;
//import com.software.levelupandroid.entity.Resource;
//import com.software.levelupandroid.entity.Task;
//import com.software.levelupandroid.taskShow.TaskShowActivity;
//import com.software.levelupandroid.utils.ToastUtil;
//
//import java.util.ArrayList;
//import java.util.List;
//
///**
// * @Description TODO
// * @Author LiangYaJie
// * @CreateTime 2024年05月30日 08:21:29
// */
//public class TaskByPriorityFragment extends Fragment {
//
//    private TaskByPriorityViewModel taskByPriorityViewModel;
//    private List<Task> tasks = new ArrayList<>();
//
//    @Nullable
//    @Override
//    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        View view = inflater.inflate(R.layout.fragment_task_by_priority, container, false);
//
//        initViewModel();
//        observeLiveData();
//
//        //TODO 调用接口
//      taskByPriorityViewModel.getTasksByPriority(getActivity(), "date", 0);
//
//        return view;
//    }
//
//    /**
//     * 初始化ViewModel
//     */
//    private void initViewModel() {
//        ViewModelProvider viewModelProvider = new ViewModelProvider(this);
//        taskByPriorityViewModel = viewModelProvider.get(TaskByPriorityViewModel.class);
//    }
//
//    /**
//     * 观测LiveData中的tasks变化
//     */
//    private void observeLiveData() {
//        taskByPriorityViewModel.getTaskListLiveData().observe(this, new Observer<Resource<List<Task>>>() {
//            @Override
//            public void onChanged(Resource<List<Task>> listResource) {
//                if (listResource != null){
//                    if (listResource.isSuccess()){
//                       //TODO 已经获得tasks，待通过Adapter加载
//
//                        List<Task> taskList = listResource.getData();
//                    }else if (listResource.isFailure()){
//                        ToastUtil.showToastOnUI(getActivity(), listResource.getErrorMessage());
//                    } else if (listResource.isLoading()) {
//
//                    }
//                }
//            }
//        });
//    }
//}

package com.software.levelupandroid.home.ui.taskByPriority;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.software.levelupandroid.R;

import com.software.levelupandroid.entity.Resource;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.home.ui.taskByPriority.adapter.GridAdapter;
import com.software.levelupandroid.home.ui.todayTask.TodayTaskViewModel;
import com.software.levelupandroid.utils.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @Description TODO
 * @Author LiangYaJie
 * @CreateTime 2024年05月30日 08:21:29
 */
public class TaskByPriorityFragment extends Fragment {

    private TodayTaskViewModel todayTaskViewModel;
    private GridAdapter gridAdapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_task, container, false);

        TextView textViewDayOfWeek =  view.findViewById(R.id.time1);
        TextView textViewDate = view.findViewById(R.id.time2);
        //获取当前时间
        Date currentDate = new Date();
        SimpleDateFormat dayOfWeekFormat = new SimpleDateFormat("EEEE", Locale.getDefault());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/M/d", Locale.getDefault());
        //设置TextView的文本
        textViewDayOfWeek.setText(dayOfWeekFormat.format(currentDate));
        textViewDate.setText(dateFormat.format(currentDate));
        textViewDayOfWeek.setTextSize(20);
        textViewDate.setTextSize(20);

        // 获取GridView引用并初始化GridAdapter
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) GridView gridView = view.findViewById(R.id.gridView);
        gridAdapter = new GridAdapter(getContext(),null);
        gridView.setAdapter(gridAdapter);

        initViewModel();
        observeLiveData();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(calendar.YEAR);
        int month = calendar.get(calendar.MONTH)+1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String date = null;
        if(month<10&&day>10){
            date = year+"年0"+month+"月"+day+"日";
        } else if (month>10&&day<10) {
            date = year+"年"+month+"月0"+day+"日";
        } else if (month<10&&day<10) {
            date = year+"年0"+month+"月0"+day+"日";
        }

        //TODO 调用接口
        todayTaskViewModel.getTasksByDate(getActivity(), "2024年06月07日");

        System.out.println(date);

        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        gridAdapter.notifyDataSetChanged();
    }
    /**
     * 初始化ViewModel
     */
    private void initViewModel() {
        ViewModelProvider viewModelProvider = new ViewModelProvider(this);
        todayTaskViewModel = viewModelProvider.get(TodayTaskViewModel.class);
    }

    /**
     * 观测LiveData中的tasks变化
     */
    private void observeLiveData() {
        todayTaskViewModel.getTaskListLiveData().observe(this, new Observer<Resource<List<Task>>>() {
            @Override
            public void onChanged(Resource<List<Task>> listResource) {
                if (listResource != null){
                    if (listResource.isSuccess()){
                        //TODO 已经获得tasks，待通过Adapter加载
                        List<Task> tasks = listResource.getData();
                        System.out.println("11111111111tasks:"+tasks);
                        gridAdapter.updateTask(tasks);
                    }else if (listResource.isFailure()){
                        ToastUtil.showToastOnUI(getActivity(), listResource.getErrorMessage());
                    } else if (listResource.isLoading()) {
                    }
                }
            }
        });
    }
}
